package i2.application.checklist.entities;

import static javax.persistence.GenerationType.AUTO;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "reponse")
public class Reponse implements Serializable {
	
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = AUTO)
	@Column(name = "id", unique = true, nullable = false)
	private int id;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "question_id", nullable = false)
	private Question question;
		
	@Column(name = "value")
	private Boolean value = false;
	
	
	//TODO faut-il que Reponse ait connaissance de ça ?
	@ManyToOne
	@JoinColumn(name = "checklist_id", nullable = false)
	private CheckList checkList;
	
	public Reponse() {
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	public boolean isValue() {
		return value;
	}

	public void setValue(boolean value) {
		this.value = value;
	}
	
	
	//FIXME ne sert à rien
	public void setValue(String value) {
		if (value.equalsIgnoreCase("oui")) {
			this.value = true;
		} else {
			this.value = false;
		}
	}
	
	public CheckList getCheckList() {
		return checkList;
	}

	public void setCheckList(CheckList checkList) {
		this.checkList = checkList;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Reponse other = (Reponse) obj;
		if (id != other.id) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "Reponse [id=" + id + ", question=" + question + ", value="
				+ value + "]";
	}

	public boolean isValid() {
		if (question.isYesIsAGoodResponse() == value) {
			return true;
		} else {
			return false;
		}
		
	}
	

}
